package task

import event.ReplenishCompleteEvent
import ext.log
import manager.AppManager
import manager.HttpRequestManager
import manager.ReplenishManager
import org.greenrobot.eventbus.EventBus
import org.json.JSONArray
import org.json.JSONObject

class ReplenishCompleteTask : Runnable {

    override fun run() {
        try {
            exec()
        } catch (e: Exception) {
            e.printStackTrace()
            EventBus.getDefault().post(ReplenishCompleteEvent(false))
        }
    }

    private fun exec() {
        AppManager.clearAllFault()
        val json = JSONObject()
        json.put("macAddr", AppManager.MacAddress)
        val arr = JSONArray()
        for (info in ReplenishManager.ReplenishList) {
            arr.put(info.toJson())
        }
        json.put("replenInfos", arr)
        val content = json.toString()
        val result = HttpRequestManager.replenishComplete(content)
        log("---$result")
        EventBus.getDefault().post(ReplenishCompleteEvent(true))
    }
}